package com.xgg.taiwuinfo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.xgg.taiwuinfo.R;
import com.xgg.taiwuinfo.filter.FC_ContainEdit;
import com.xgg.taiwuinfo.filter.FC_SelectCompareNum;
import com.xgg.taiwuinfo.filter.FilterComponent;

import java.util.ArrayList;

public class FCAdapter extends SimpleAdapter
{
    private LayoutInflater layoutInflater;
    private Context m_ctx;
    public ArrayList<FilterComponent> m_fclist = new ArrayList<>();

    public FCAdapter(Context context,  int resource, String[] from, int[] to)
    {
        super(context, null, resource, from, to);
        m_ctx = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (m_fclist == null)
            return 0;
        else
            return m_fclist.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2)
    {
        FilterComponent fc = m_fclist.get(position);
        int itemtype = fc.getType() / 10000;

        //if(convertView == null)
        {
            int res = fc.getItemLayout();
            convertView = layoutInflater.inflate(res , null);
        }
        Button btn_delete = convertView.findViewById(R.id.btn_delete);
        btn_delete.setOnClickListener(fc.delete_listener);

        if (itemtype == 1)
        {
            Spinner spin_value = convertView.findViewById(R.id.spin_value);
            spin_value.setAdapter(fc.getValueAdapter(m_ctx));
            spin_value.setOnItemSelectedListener(fc.value_listener);
            fc.selectValue(spin_value);

            Spinner spin_compare = convertView.findViewById(R.id.spin_compare);
            spin_compare.setAdapter(fc.getCompareAdapter(m_ctx));
            spin_compare.setOnItemSelectedListener(fc.compare_listener);
            fc.selectCompare(spin_compare);

            String field = fc.getFieldString(m_ctx).get(0).get("f");
            TextView txt_field = convertView.findViewById(R.id.txt_field);
            txt_field.setText(field);
        }
        else if (itemtype == 2)
        {
            Spinner spin_value = convertView.findViewById(R.id.spin_value);
            spin_value.setAdapter(fc.getValueAdapter(m_ctx));
            spin_value.setOnItemSelectedListener(fc.value_listener);
            fc.selectValue(spin_value);

            Spinner spin_compare = convertView.findViewById(R.id.spin_compare);
            spin_compare.setAdapter(fc.getCompareAdapter(m_ctx));
            spin_compare.setOnItemSelectedListener(fc.compare_listener);
            fc.selectCompare(spin_compare);

            String field = fc.getFieldString(m_ctx).get(0).get("f");
            TextView txt_field = convertView.findViewById(R.id.txt_field);
            txt_field.setText(field);
        }
        else if (itemtype == 3){
            Spinner spin_field = convertView.findViewById(R.id.spin_field);
            spin_field.setAdapter(fc.getFieldAdapter(m_ctx));
            spin_field.setOnItemSelectedListener(fc.field_listener);
            fc.selectField(spin_field);

            Spinner spin_compare = convertView.findViewById(R.id.spin_compare);
            spin_compare.setAdapter(fc.getCompareAdapter(m_ctx));
            spin_compare.setOnItemSelectedListener(fc.compare_listener);
            fc.selectCompare(spin_compare);

            Spinner spin_value = convertView.findViewById(R.id.spin_value);
            spin_value.setAdapter(fc.getValueAdapter(m_ctx));
            spin_value.setOnItemSelectedListener(fc.value_listener);
            fc.selectValue(spin_value);
        }
        else if (itemtype == 4)
        {
            FC_ContainEdit fcce = (FC_ContainEdit)fc;

            EditText edt_value = convertView.findViewById(R.id.edt_value);
            edt_value.addTextChangedListener(fc.text_listener);
            edt_value.setText(fcce.TempValue);

            String field = fc.getFieldString(m_ctx).get(0).get("f");
            TextView txt_field = convertView.findViewById(R.id.txt_field);
            txt_field.setText(field);
        }
        else if (itemtype == 5)
        {
            Spinner spin_field = convertView.findViewById(R.id.spin_field);
            spin_field.setAdapter(fc.getFieldAdapter(m_ctx));
            spin_field.setOnItemSelectedListener(fc.field_listener);
            fc.selectField(spin_field);

            Spinner spin_compare = convertView.findViewById(R.id.spin_compare);
            spin_compare.setAdapter(fc.getCompareAdapter(m_ctx));
            spin_compare.setOnItemSelectedListener(fc.compare_listener);
            fc.selectCompare(spin_compare);

            FC_SelectCompareNum fccn = (FC_SelectCompareNum)fc;

            EditText edt_value = convertView.findViewById(R.id.edt_value);
            edt_value.addTextChangedListener(fc.text_listener);
            edt_value.setText(fccn.TempValue);
        }
        return convertView;
    }


}
